package com.itany.corejava.day11;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Enumeration;

public class TestForm {

    public static void main(String[] args) {
        JFrame jf=new JFrame();
        jf.setLayout(new FlowLayout());


        //文本框
        JTextField jtf=new JTextField(10);
        jf.add(jtf);

        //密码框
        JPasswordField jpf=new JPasswordField(10);
        jpf.setEchoChar('*');
        jf.add(jpf);

        //文本域
        JTextArea jta=new JTextArea(15,30);
        JScrollPane jsp=new JScrollPane(jta);
        jf.add(jsp);

        //复选框
        JCheckBox jcb1=new JCheckBox("a");
        JCheckBox jcb2=new JCheckBox("b");
        JCheckBox jcb3=new JCheckBox("c");
        jf.add(jcb1);
        jf.add(jcb2);
        jf.add(jcb3);

        //单选按钮
        JRadioButton jrb1=new JRadioButton("a");
        JRadioButton jrb2=new JRadioButton("b");
        ButtonGroup bg=new ButtonGroup();
        bg.add(jrb1);
        bg.add(jrb2);
        jf.add(jrb1);
        jf.add(jrb2);

        //下拉列表
        JComboBox jcb=new JComboBox();
        String[] items={"---请选择---","a","b"};
        for (String item : items) {
            jcb.addItem(item);
        }
        jf.add(jcb);

        //文件选择器
        JFileChooser jfc=new JFileChooser();
        jf.add(jfc);


        JButton jb=new JButton("提交");
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("文本框");
                System.out.println(jtf.getText());
                System.out.println("密码框");
                System.out.println(jpf.getText());
                System.out.println(Arrays.toString(jpf.getPassword()));
                System.out.println("文本域");
                System.out.println(jta.getText());
                System.out.println("复选框");
                System.out.println(jcb1.isSelected());
                System.out.println(jcb2.isSelected());
                System.out.println(jcb3.isSelected());
                System.out.println("单选按钮");
                System.out.println(jrb1.isSelected());
                System.out.println(jrb2.isSelected());
                Enumeration<AbstractButton> buttons=bg.getElements();
                while(buttons.hasMoreElements()){
                    AbstractButton button= buttons.nextElement();
                    if(button.isSelected()){
                        System.out.println(button.getText());
                    }
                }
                System.out.println("下拉列表");
                System.out.println(jcb.getSelectedIndex());
                System.out.println(jcb.getSelectedItem());
            }
        });
        jf.add(jb);



        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    }


}
